import axios from "axios";
import router from "@/router";

const request = axios.create({
    baseURL:'http://localhost:9090',
    timeout:10000
})
//request 拦截器
//可以在请求前对请求做一些处理，比如统一加token，加密等

request.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=utf-8';
    let user = localStorage.getItem("user_data") ? JSON.parse(localStorage.getItem("user_data")) : {}
    if (user) {
        config.headers['token'] = user.token;  // 设置请求头
    }

    return config
},
    error => {
    console.log(error)
        return Promise.reject(error)
    });
request.interceptors.response.use(response => {
    let res = response.data;
    if(typeof res === 'string'){
        res = ress?JSON.parse(res) : res;
    }
    if(res.code === '401'){
        router.push('/login');
    }
    return res;
},
    error => {
        console.log(error)
        return Promise.reject(error)
    })
export default request;
